package com.njlime.sys.entities;

import com.njlime.core.base.entities.BaseEntity;
import com.njlime.core.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
import java.util.Map;

/**
 * 系统日志实体
 * Created by Dean on 2017-7-18.
 */
@Entity
@Table(name = "t_log")
public class Log extends BaseEntity {
    private Integer type; //日志类型 1正常 2错误
    private String title; //日志标题
    private Integer operator; //操作者
    private Date operate_time; //操作时间
    private String remote_addr; //IP地址
    private String user_agent; //用户代理
    private String request_uri; //请求url
    private String method; //提交方法
    private String params; //参数
    private String exception; //异常信息

    /**
     * 日志类型
     * TYPE_NORMAL 正常日志
     * TYPE_EXCEPTION 异常日志
     */
    public static final class LOG_TYPE {
        public static final Integer TYPE_NORMAL = 1;
        public static final Integer TYPE_EXCEPTION = 2;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getOperator() {
        return operator;
    }

    public void setOperator(Integer operator) {
        this.operator = operator;
    }

    public Date getOperate_time() {
        return operate_time;
    }

    public void setOperate_time(Date operate_time) {
        this.operate_time = operate_time;
    }

    public String getRemote_addr() {
        return remote_addr;
    }

    public void setRemote_addr(String remote_addr) {
        this.remote_addr = remote_addr;
    }

    public String getUser_agent() {
        return user_agent;
    }

    public void setUser_agent(String user_agent) {
        this.user_agent = user_agent;
    }

    public String getRequest_uri() {
        return request_uri;
    }

    public void setRequest_uri(String request_uri) {
        this.request_uri = request_uri;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getParams() {
        return params;
    }

    public void setParams(Map paramMap){
        if (paramMap == null){
            return;
        }
        StringBuilder params = new StringBuilder();
        for (Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){
            params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(StringUtil.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }
}
